package com.app.knock.db;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import z.lib.base.CommonAndroid;
import z.lib.base.LogUtils;
import z.lib.base.BaseUtils;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class IconList extends SkypeTable {
	public IconList(Context context) {
		super(context);
		addColumns(id);
		addColumns(url);
	}

	public static final String id = "id";
	public static final String url = "url";
	public static final String icon = "icon";
	private static final String TAG = "IconList";

	@Override
	public int getIndex() {
		return 3;
	}

	public void updateIconList(String response) {
//		String user_idStr = new Account(getContext()).getUserId();
		try {
			JSONObject json = new JSONObject(response);
			String is_succes = CommonAndroid.getString(json, BaseUtils.KEY.is_success);

			if (BaseUtils.VALUE.STATUS_API_SUCCESS.equals(is_succes)) {
				JSONArray array = json.getJSONObject("data").getJSONArray("icons");

				for (int i = 0; i < array.length(); i++) {
					JSONObject object = array.getJSONObject(i);
					String idStr = CommonAndroid.getString(object, id);

					if (!CommonAndroid.isBlank(idStr)) {
						String where = String.format("%s = '%s' ", id, idStr );
						ContentValues values = new ContentValues();

						values.put(id, CommonAndroid.getString(object, id));
						values.put(url, CommonAndroid.getString(object, url));

						if (has(where)) {
							getContext().getContentResolver().update(getContentUri(), values, where, null);
						} else {
							getContext().getContentResolver().insert(getContentUri(), values);
						}
					}
				}
			}
		} catch (Exception e) {
		}
	}
	
	public Cursor querryIcon(String id2) {
		String where = String.format("%s = '%s' ", id, id2 );
		return querry(where);
	}

	public String getIcon(String id) {
		String suser_id = "";
		try {
			Cursor cursor = querryIcon(id);
			if (cursor != null) {
				if (cursor.moveToNext()) {
					suser_id = CommonAndroid.getString(cursor, url);
				}
				cursor.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return suser_id;
	}

}
